define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tickets/index' + location.search,
                    add_url: 'tickets/add',
                    edit_url: 'tickets/edit',
                    del_url: 'tickets/del',
                    multi_url: 'tickets/multi',
                    import_url: 'tickets/import',
                    table: 'tickets',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'payment_time', title: __('payment_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'notice_time', title: __('公众号通知时间'), sortable: true, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'openid', title: __('Openid'), operate: 'LIKE' },
                        { field: 'order_id', title: __('Order_id') },
                        { field: 'user_id', title: __('User_id') },
                        { field: 'address', title: __('Address'), operate: 'LIKE' },
                        { field: 'address_id', title: __('Address_id'), operate: 'LIKE' },
                        { field: 'area', title: __('Area'), operate: 'LIKE' },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        { field: 'card_number', title: __('Card_number'), operate: 'LIKE' },
                        { field: 'city', title: __('City'), operate: 'LIKE' },
                        { field: 'fee_area', title: __('Fee_area'), operate: 'LIKE' },
                        { field: 'floor_area', title: __('Floor_area'), operate: 'LIKE' },
                        { field: 'heating_company', title: __('Heating_company'), operate: 'LIKE' },
                        { field: 'heating_way', title: __('Heating_way'), operate: 'LIKE' },
                        { field: 'invoice_no', title: __('Invoice_no'), operate: 'LIKE' },
                        { field: 'money', title: __('Money'), operate: 'LIKE' },
                        { field: 'num', title: __('Num'), operate: 'LIKE' },
                        { field: 'order_no', title: __('Order_no'), operate: 'LIKE' },
                        { field: 'pay_annual', title: __('Pay_annual'), operate: 'LIKE' },
                        { field: 'phone', title: __('Phone'), operate: 'LIKE' },
                        { field: 'province', title: __('Province'), operate: 'LIKE' },
                        { field: 'price', title: __('Price'), operate: 'BETWEEN' },
                        { field: 'room_number', title: __('Room_number'), operate: 'LIKE' },
                        { field: 'source', title: __('Source'), operate: 'LIKE' },
                        { field: 'type', title: __('Type'), operate: 'LIKE' },
                        { field: 'unionid', title: __('Unionid'), operate: 'LIKE' },
                        { field: 'unit_name', title: __('Unit_name'), operate: 'LIKE' },
                        { field: 'unit_number', title: __('Unit_number'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE' },
                        { field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE' },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            //获取选中项
            $(document).on("click", ".btn-selecteds", function () {
                //在templateView的模式下不能调用table.bootstrapTable('getSelections')来获取选中的ID,只能通过下面的Table.api.selectedids来获取
                // Layer.alert(JSON.stringify(Table.api.selectedids(table)));
                Backend.api.ajax({
                    url: "tickets/send/ids/" + Table.api.selectedids(table),
                    data: { name: '' }
                }, function (res, data) {
                    console.log(res, data)
                    Toastr.success(data.msg);
                    $(".btn-refresh").trigger("click");
                    return false;
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tickets/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), align: 'left' },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tickets/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tickets/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
